package Boke2;

import java.util.Scanner;

public class Project1 {
    public  static Object loker=new Object();
    //结果值
    public static int cout=0;
    //修改值
    public static int update;
    public static void main(String[] args) {
        System.out.println("初始cout值"+cout);
        System.out.println("请输入你要修改的cout值");
        Scanner scanner=new Scanner(System.in);
        update=scanner.nextInt();
        //创建线程t1进行计算
        Thread t1 = new Thread(() -> {
            try {
                //此处让t1休眠的原因是保证t2先进入阻塞等待
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized(loker){
                    System.out.println("t1正在计算cout值");
                    cout=update;
                    loker.notify();
                    System.out.println("t1计算结束");
            }

        });
        t1.start();

        //创建线程t2进行读取最新的cout值
        Thread t2=new Thread(()->{
            synchronized (loker){
                try {
                    loker.wait();
                    System.out.println("t2获取最新cout值为"+cout);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        });
        t2.start();
    }
}
